package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.siyamed.shapeimageview.CircularImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.HttpUtil;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.view.ToastCustom;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2015/1/22.
 */
public class UptoHairResetActivity extends Activity implements View.OnClickListener {

    private LinearLayout detail,progressDetail;
    private RelativeLayout send;
    private EditText organ,account;
    private ImageView back;
    private TextView masterName,find,progressTxt,address;
    private CircularImageView masterHead;
    private Map<String,String> map;
    private UptoHairResetHandler handler;
    private boolean isUpToHairReset = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            throw new NullPointerException();
        }
        this.setContentView(R.layout.page_up_to_hairreset);

        init();

    }


    void init(){


        HttpUtil.init(getApplicationContext());
        ImageLoaderApi.initImageLoaderApi(getApplicationContext());
        initView();

    }

    void initView(){

        send = (RelativeLayout)findViewById(R.id.up_hairreset_send);
        organ = (EditText)findViewById(R.id.up_hairreset_name);
        account = (EditText)findViewById(R.id.up_hairreset_account);
        detail = (LinearLayout)findViewById(R.id.up_hairreset_detail);
        progressDetail = (LinearLayout)findViewById(R.id.up_hairreset_progress_layout);
        back = (ImageView)findViewById(R.id.up_hairreset_back);
        masterName = (TextView)findViewById(R.id.up_hairreset_master_name);
        progressTxt = (TextView)findViewById(R.id.up_hairreset_txt);
        find = (TextView)findViewById(R.id.up_hairreset_find);
        masterHead = (CircularImageView)findViewById(R.id.up_hairreset_master_head);
        address = (TextView)findViewById(R.id.up_hairreset_master_address);
        handler = new UptoHairResetHandler();

        send.setOnClickListener(this);
        back.setOnClickListener(this);
        find.setOnClickListener(this);

//        organ.setText("大宝");
//        account.setText("13901117587");


    }

    int legal(){

        if(organ.getText().toString().trim().length() == 0 || organ.getText().toString().trim().length() > 20 )
            return 0;
        if(account.getText().toString().trim().length() == 0 || account.getText().toString().trim().length() > 20)
            return 1;

        return 10;

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.up_hairreset_back://点击了退后

                UptoHairResetActivity.this.finish();

                break;

            case R.id.up_hairreset_find://点击了查看
            case R.id.up_hairreset_send://点击了提交

                switch (legal()){

                    case 0:
                        ToastCustom.showToast(getApplicationContext(),"门店名称不正确");
                        break;
                    case 1:
                        ToastCustom.showToast(getApplicationContext(),"店主账号不正确");
                        break;

                    default:

                        if(view.getId() == R.id.up_hairreset_find){//点击了查看

                            if(!isUpToHairReset){
                                isUpToHairReset = true;
                                progressDetail.setVisibility(View.VISIBLE);
                                progressTxt.setText("正在查询，请稍等...");

                                map = new HashMap<String,String>();
                                map.put("storeName",organ.getText().toString().trim());
                                map.put("storekeeperAccount",account.getText().toString().trim());

                                HttpUtil.apiFindOrganMessage(map,handler);
                            }

                        }else{//点击发送

                            if(!isUpToHairReset){
                                isUpToHairReset = true;
                                progressDetail.setVisibility(View.VISIBLE);
                                progressTxt.setText("正在提交，请稍等...");
                                map = new HashMap<String,String>();
                                map.put("storeName",organ.getText().toString().trim());
                                map.put("storekeeperAccount",account.getText().toString().trim());

                                HttpUtil.apiUpToHariReset(map,handler);
                            }



                        }
                        break;
                }
                break;
        }

    }


    class UptoHairResetHandler extends Handler {

        @Override
        public void dispatchMessage(Message msg) {


            switch (msg.what){

                case HttpUtil.RESET_UP_TO_HAIRRESET_SUCCESS:

                    ToastCustom.showToast(getApplicationContext(),"升级成理发师成功!");

                    progressDetail.setVisibility(View.GONE);
                    setResult(RESULT_OK);
                    FileUtil.deleteFile(new File(FileUtil.detailCachePath));
                    FileUtil.deleteFile(new File(FileUtil.storeActiveCachePathName));
                    UptoHairResetActivity.this.finish();

                    break;

                case HttpUtil.RESET_UP_TO_HAIRRESET_FAILD:
                case HttpUtil.GET_STORE_MESSAGE_FAILD:

                    if(msg.obj != null){
                        if(msg.obj.toString().equals("100")){//未登录
                                HttpUtil.reLogin(getApplication(),handler);
                            }else if(msg.obj.toString().equals("108")){
                                ToastCustom.showToast(getApplicationContext(),"店铺名称或账号不正确");
                            }else{
                                ToastCustom.showToast(getApplicationContext(),"连接服务器错误，请稍后再试!");
                            }
                    }else{
                        ToastCustom.showToast(getApplicationContext(),"连接服务器错误，请稍后再试!");
                    }

                    isUpToHairReset = false;
                    progressDetail.setVisibility(View.GONE);
                    break;

                case HttpUtil.LOGIN_SUCCESS:

                    HttpUtil.apiUpToHariReset(map,handler);

                    break;

                case HttpUtil.GET_STORE_MESSAGE_SUCCESS:

                    if(msg.obj != null){
                        Map<String,String> tempMap = (Map<String,String>)msg.obj ;

                        ImageLoader.getInstance().displayImage(Common.baseUrl + tempMap.get("head"), masterHead, ImageLoaderApi.getOptions());

                        if(!tempMap.get("nickName").equals(""))
                             masterName.setText(tempMap.get("nickName"));
                        else if(!tempMap.get("name").equals(""))
                             masterName.setText(tempMap.get("name"));
                        else masterName.setText(account.getText().toString().trim());
                        address.setText(tempMap.get("address"));

                        detail.setVisibility(View.VISIBLE);


                    }

                    isUpToHairReset = false;
                    progressDetail.setVisibility(View.GONE);

                    break;


                case HttpUtil.LOGIN_FAILD:

                    ToastCustom.showToast(getApplicationContext(),"请重新登录!");
                    progressDetail.setVisibility(View.GONE);
                    isUpToHairReset = false;

                    break;



            }

        }
    }

}
